/*
 * Copyright (c) Scrap Material Junior Brother, Xin. 2023-2023. All rights reserved.
 */

package array;

import util.BaseUtils;

import java.util.*;

/**
 * 数组常用API
 *
 * @author xin
 * @since 2023-04-29
 */
public class ArrayApi {
    private static void declare() {
        String[] aArr = new String[3];
        aArr[0] = "a";
        String[] bArr = {"a", "b", "c"};
        String[] cArr = new String[]{"a", "b", "c"};
        System.out.println(Arrays.toString(aArr));
        System.out.println(Arrays.toString(bArr));
        System.out.println(Arrays.toString(cArr));
    }

    private static void arrToList() {
        String[] arr = {"a", "b", "c"};
        List<String> list = new ArrayList<>(Arrays.asList(arr));
        Set<String> set = new HashSet<>(Arrays.asList(arr));
        System.out.println(list);
        System.out.println(set);
    }

    private static void isExist() {
        String[] arr = {"a", "b", "c"};
        int index = Arrays.binarySearch(arr, "a");
        int index2 = Arrays.binarySearch(arr, "f");
        boolean isExist = Arrays.asList(arr).contains("a");
        System.out.printf("isExist: %b, index: %d, index2: %d\n", isExist, index, index2);
    }

    private static void merge() {
        String[] arr1 = {"a", "b", "c"};
        String[] arr2 = {"d", "e", "f"};
        String[] mergeArr = new String[arr1.length + arr2.length];
        System.arraycopy(arr1, 0, mergeArr, 0, arr1.length);
        System.arraycopy(arr2, 0, mergeArr, arr1.length, arr2.length);
        System.out.println(Arrays.toString(mergeArr));
    }

    private static void arrToString() {
        String[] arr = {"a", "b", "c"};
        System.out.println(String.join("", arr));
    }

    private static void listToArr() {
        String[] arr = {"a", "b", "c"};
        List<String> list = new ArrayList<>(Arrays.asList(arr));
        String[] listToArr = new String[list.size()];
        list.toArray(listToArr);
        System.out.println(listToArr[0]);
        System.out.println(Arrays.toString(listToArr));
        System.out.println(Arrays.toString(list.toArray()));
    }

    private static void reverse() {
        String[] arr = {"a", "b", "c"};
        List<String> list = new ArrayList<>(Arrays.asList(arr));
        Collections.reverse(list);
        System.out.println(Arrays.toString(list.toArray()));
    }

    private static void remove() {
        String[] arr = {"a", "b", "c", "d", "e", "d"};
        // 删除指定元素
        System.out.println(Arrays.toString(Arrays.stream(arr).filter(item -> !"d".equals(item)).toArray()));
        // 删除指定坐标元素，可能会越界，且迭代删除可能存在并发问题
        List<String> list1 = new ArrayList<>(Arrays.asList(arr));
        list1.remove(3);
        System.out.println(Arrays.toString(list1.toArray()));
        // 删除指定元素
        List<String> list2 = new ArrayList<>(Arrays.asList(arr));
        list2.removeIf("d"::equals);
        System.out.println(Arrays.toString(list2.toArray()));
    }

    private static void fill() {
        String[] arr1 = new String[3];
        String[] arr2 = new String[3];
        Arrays.fill(arr1, "a");
        Arrays.fill(arr2, 0, 1, "a");
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
    }

    private static void compare() {
        String[] arr1 = {"a", "b", "c"};
        String[] arr2 = {"a", "b", "d"};
        System.out.println(Arrays.equals(arr1, arr2));
    }

    private static void sort() {
        String[] arr1 = {"a", "ac", "ab"};
        Arrays.sort(arr1);
        System.out.println(Arrays.toString(arr1));
        String[] arr2 = {"a", "ac", "ab"};
        Arrays.sort(arr2, 1, 3);
        System.out.println(Arrays.toString(arr2));
    }

    private static void strToCharArr() {
        String str = "abc";
        char[] chars = str.toCharArray();
        System.out.println(Arrays.toString(chars));
    }

    public static void main(String[] args) {
        BaseUtils.print("declare", ArrayApi::declare);
        BaseUtils.print("arrToList", ArrayApi::arrToList);
        BaseUtils.print("isExist", ArrayApi::isExist);
        BaseUtils.print("merge", ArrayApi::merge);
        BaseUtils.print("arrToString", ArrayApi::arrToString);
        BaseUtils.print("listToArr", ArrayApi::listToArr);
        BaseUtils.print("reverse", ArrayApi::reverse);
        BaseUtils.print("remove", ArrayApi::remove);
        BaseUtils.print("fill", ArrayApi::fill);
        BaseUtils.print("compare", ArrayApi::compare);
        BaseUtils.print("sort", ArrayApi::sort);
        BaseUtils.print("strToCharArr", ArrayApi::strToCharArr);
    }
}
